package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Order;
import com.itheima.service.OrderService;
import com.itheima.utils.HealthStringUtils;
import com.itheima.utils.QuerySendDetailsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @description: 预约web层
 * @author: wangjingliang
 * @create: 2019-11-19 17:11
 **/
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private RedisTemplate redisTemplate;
    @Reference
    private OrderService orderService;

    /**
     * url:/order/submit.do",this.orderInfo
     * 对预约提交的信息进行校验并处理
     *
     * @param:
     * @return: null
     * @date: 2019/11/19
     */
    @PostMapping("/submit")
    public Result submit(@RequestBody Map map) {
        //获取前传来的手机号
        String telephone_old = (String) map.get("telephone");
        if (HealthStringUtils.isEmpty(telephone_old)) {
            return new Result(false, "手机号码必须填写");
        }
        String telephone = telephone_old + RedisMessageConstant.SENDTYPE_ORDER;
        String validateCode4String = null;

            //根据手机号从redis中获取验证码
            validateCode4String = (String) redisTemplate.boundValueOps(telephone).get();

        if (HealthStringUtils.isEmpty(validateCode4String)) {
            return new Result(false, "验证码未发生或已过期");
        }
        //对输入的验证码进行判断
        String validateCode = (String) map.get("validateCode");
        //对验证码进行判断
        if (HealthStringUtils.isEmpty(validateCode)) {
            return new Result(false, "验证码有误");
        }
        //校验验证码
        if (!validateCode.equalsIgnoreCase(validateCode4String)) {
            return new Result(false, MessageConstant.VALIDATECODE_ERROR);
        }

        //验证码正确,添加预约类型为微信预约,并传入后台
        map.put("orderType", Order.ORDERTYPE_WEIXIN);

        //传入后台并获取返回信息
        Map orderMap = null;
        try {
            orderMap = orderService.order(map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, e.getMessage());
        }
        if (orderMap != null) {
            //预约成功后返送短信,通知用户具体的时间地点和套餐
            try {
                QuerySendDetailsUtils.SendSms(telephone_old, orderMap.get("member"), orderMap.get("orderDate"), orderMap.get("setmeal"), QuerySendDetailsUtils.ORDER_NOTICE);
                System.out.println((String) map.get("orderDate") + "," + orderMap.get("setmealName"));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return new Result(true, MessageConstant.ORDERSETTING_SUCCESS, orderMap.get("orderId"));
    }


    /**
     * url:/order/findById.do?id
     *
     * @param:
     * @return: null
     * @date: 2019/11/20
     */
    @PostMapping("/findById")
    public Result findById(Integer id) {
        Map map = null;
        try {
            map = orderService.findById(id);
            return new Result(true, MessageConstant.QUERY_ORDER_SUCCESS, map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ORDER_FAIL, e.getMessage());
        }

    }
}
